package com.fh.controller.cuijimanage;

import com.alibaba.fastjson.JSON;
import com.fh.controller.base.BaseController;
import com.fh.model.loan.CallLoanCallInfo;
import com.fh.model.loan.CallLoanDeviceInfo;
import com.fh.model.loan.HisOverdueBillInfo;
import com.fh.model.loan.UserContactsInfo;
import com.fh.service.cuijimanage.HisOverdueBillInfoService;
import com.fh.service.cuijimanage.UserContactsInfoService;
import com.fh.util.CoverUtil;
import com.fh.util.EhcacheUtil;
import com.fh.util.ResultJSON;
import com.ybd.common.component.redis.StringRedisUtil;
import com.ybd.common.mask.MaskTypeEnum;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created by dell、 on 2017/6/15.
 */
@Controller
@RequestMapping(value = "/cuijisecondtimes")
public class CuijiSecondTimesController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(CuijiSecondTimesController.class);

    @Autowired
    private HisOverdueBillInfoService hisOverdueBillInfoService;
    @Autowired
    private UserContactsInfoService userContactsInfoService;
   /* @Autowired
    private Jiucache jiucache;*/
   @Resource
   private StringRedisUtil stringRedisUtil;
    
    //获取历史逾期账单
    @RequestMapping(value = "/gethisoverduebillinfo.do")
    @ResponseBody
    public List<HisOverdueBillInfo> getHisOverdueBillInfo(HttpServletRequest request) throws Exception {
        String userId = request.getParameter("userId");
        String callLoanNid = request.getParameter("callLoanNid");
        Map<String, Object> pramsmap = new HashMap<>();
        pramsmap.put("userId", userId);
        pramsmap.put("callLoanNid", callLoanNid);
        logger.info("获取历史逾期账单入参==>{}",JSON.toJSON(pramsmap));
        List<HisOverdueBillInfo> list = hisOverdueBillInfoService.findHisOverdueBillInfo(pramsmap);
        logger.info("获取历史逾期账单返回==>{}",JSON.toJSON(list));
        return list;
    }

    //获取最近3天通话记录，上下位置的中间的表
    @RequestMapping(value = "/getLast3daysCallRecord.do")
    @ResponseBody
    public List<CallLoanCallInfo> getLast3daysCallRecord(HttpServletRequest request) throws Exception {
        String userId = request.getParameter("userId");
        List<CallLoanCallInfo> list = hisOverdueBillInfoService.getLast3DaysCallRecord(userId);
        return list;
    }

//获取设备登录信息
    @RequestMapping(value = "/getDeviceLoginInfo.do")
    @ResponseBody
    public List<CallLoanDeviceInfo> getDeviceLoginInfo(HttpServletRequest request) throws Exception {
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        logger.info("获取设备登录信息，userId==>{}",userId);
        String flag = request.getParameter("flag");
       // EhcacheUtil ehCache = EhcacheUtil.getInstance();
        List<CallLoanDeviceInfo> list = null;
        try {
          //  Object obj = jiucache.getData("deviceLogin" + userId);
            Object obj = stringRedisUtil.get("deviceLogin" + userId);
            if (obj != null) {
                logger.info("获取设备登录信息,走redis缓存");
                list = (List<CallLoanDeviceInfo>) obj;
            } else {
                logger.info("获取设备登录信息,走sql");
                list = hisOverdueBillInfoService.getDevcieLoginInfo(userId);
                if (list != null) {
                    for (CallLoanDeviceInfo mao : list) {
                        String ipadress = mao.getIp();
                        mao.setIp(ipadress + "&nbsp;&nbsp;<font color='red'>" + JSON.toJSON(mao.getProvince()) + JSON.toJSON(mao.getCity()) + "</font>");
                    }
                    //  ehCache.put("deviceInfo", "deviceLogin" + userId, list);
                   // jiucache.putData("deviceLogin" + userId, list, 180, TimeUnit.SECONDS);
                  Boolean bool =  stringRedisUtil.put("deviceLogin" + userId, list, 180);
                    if(bool) {
                        logger.info("redis缓存中没有，已放入redis缓存");
                    }
                }
            }
        }catch (Exception e){
            logger.error("获取设备登录信息出错==>{}",e.getMessage());
        }
        if(!StringUtils.isEmpty(flag)&&flag.equals("nei")){
            logger.info("内催页面请求，手机号做掩码加密处理");
            Map<String,MaskTypeEnum> properties = new HashMap<>();
            properties.put("mobile",MaskTypeEnum.MOBILE);
            if(list!=null){
                //加密
                CoverUtil.setListCover(list, properties);
            }
        }else if(!StringUtils.isEmpty(flag)&&flag.equals("wai")){
            logger.info("外催页面请求手机号不做掩码加密处理");
        }
        return list;
    }
    /**
     * 修改原有联系人状态
     *
     * @param info
     * @return
     */
    @RequestMapping("updateOldContract")
    @ResponseBody
    public ResultJSON updateOldContract(UserContactsInfo info){
    	ResultJSON json = new ResultJSON();
    	logger.info("修改原有联系人状态", JSON.toJSON(info));
    	try {
    		userContactsInfoService.updateOldContract(info);
    		json.setMsg("修改成功!");
		} catch (Exception e) {
			json.setSuccess(false);
			json.setMsg("修改失败!");
			logger.error("修改原有联系人异常!",e);
		}
    	return json;
    }
}
